﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using DBUtility;

namespace XQ.SP.DalServices  
{
	 	//BaseCity
		public partial class BaseCityDal : BaseDal
	{
   		     
		public bool ExistsWhere(string whereSql)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from BaseCity");
            if (whereSql != "")
            {
                strSql.Append(" where " + whereSql);
            }
			
			return DbHelperSQL.Exists(strSql.ToString());
		}
		public bool Exists(long id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [BaseCity]");
			strSql.Append(" where  [Id]=@Id ");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;
			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(XQ.SP.Model.BaseCityItem model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [BaseCity](");			
            strSql.Append("[BaseProvinceID],[BaseCityName],[SortNumber],[Url],[Description]");
			strSql.Append(") values (");
            strSql.Append("@BaseProvinceID,@BaseCityName,@SortNumber,@Url,@Description");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@BaseProvinceID", SqlDbType.Int,4) ,            
                        new SqlParameter("@BaseCityName", SqlDbType.VarChar,40) ,            
                        new SqlParameter("@SortNumber", SqlDbType.Int,4) ,            
                        new SqlParameter("@Url", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Description", SqlDbType.NVarChar,1000)             
              
            };
			            
            parameters[0].Value = model.BaseProvinceID;                        
            parameters[1].Value = model.BaseCityName;                        
            parameters[2].Value = model.SortNumber;                        
            parameters[3].Value = model.Url;                        
            parameters[4].Value = model.Description;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		
		/// <summary>
		///   批量添加数据
		/// </summary>
		public int AddList(List<XQ.SP.Model.BaseCityItem> models)
		{
			List<CommandInfo> commands = new List<CommandInfo>();
			foreach(XQ.SP.Model.BaseCityItem item in models)
			{
				CommandInfo command = new CommandInfo();
				StringBuilder strSql=new StringBuilder();
				strSql.Append("insert into [BaseCity](");			
	            strSql.Append("[BaseProvinceID],[BaseCityName],[SortNumber],[Url],[Description]");
				strSql.Append(") values (");
	            strSql.Append("@BaseProvinceID,@BaseCityName,@SortNumber,@Url,@Description");            
	            strSql.Append(") ");            
	            strSql.Append(";select @@IDENTITY");		
				SqlParameter[] parameters = {
					            new SqlParameter("@BaseProvinceID", SqlDbType.Int,4) ,            
	            	            new SqlParameter("@BaseCityName", SqlDbType.VarChar,40) ,            
	            	            new SqlParameter("@SortNumber", SqlDbType.Int,4) ,            
	            	            new SqlParameter("@Url", SqlDbType.NVarChar,100) ,            
	            	            new SqlParameter("@Description", SqlDbType.NVarChar,1000)             
	              
	            };
	            				            
	            parameters[0].Value = item.BaseProvinceID;	                        
	            parameters[1].Value = item.BaseCityName;	                        
	            parameters[2].Value = item.SortNumber;	                        
	            parameters[3].Value = item.Url;	                        
	            parameters[4].Value = item.Description;	            	            
	         	command.CommandText = strSql.ToString();
	         	command.Parameters = parameters;
	         	
	         	commands.Add(command);
	         }
            
			object obj = DbHelperSQL.ExecuteSqlTran(commands);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
            	return Convert.ToInt32(obj);
			}
			
           
		}
		
		
		/// <summary>
		/// 批量更新数据
		/// </summary>
		public int UpdateList(List<XQ.SP.Model.BaseCityItem> models)
		{
			List<CommandInfo> commands = new List<CommandInfo>();
			CommandInfo command = new CommandInfo();
			
			foreach(XQ.SP.Model.BaseCityItem item in models)
			{
					StringBuilder strSql=new StringBuilder();
					strSql.Append("update [BaseCity] set ");
							            		            		                        
		            strSql.Append(" [BaseProvinceID] = @BaseProvinceID , ");		            		                        
		            strSql.Append(" [BaseCityName] = @BaseCityName , ");		            		                        
		            strSql.Append(" [SortNumber] = @SortNumber , ");		            		                        
		            strSql.Append(" [Url] = @Url , ");		            		                        
		            strSql.Append(" [Description] = @Description  ");		            			
					strSql.Append(" where [Id]=@Id ");
								
					SqlParameter[] parameters = {
							            new SqlParameter("@BaseCityID", SqlDbType.Int,4) ,		            
		            		            new SqlParameter("@BaseProvinceID", SqlDbType.Int,4) ,		            
		            		            new SqlParameter("@BaseCityName", SqlDbType.VarChar,40) ,		            
		            		            new SqlParameter("@SortNumber", SqlDbType.Int,4) ,		            
		            		            new SqlParameter("@Url", SqlDbType.NVarChar,100) ,		            
		            		            new SqlParameter("@Description", SqlDbType.NVarChar,1000) 		            
		              
		            };
		            					            
		            parameters[0].Value = item.BaseCityID;		                        
		            parameters[1].Value = item.BaseProvinceID;		                        
		            parameters[2].Value = item.BaseCityName;		                        
		            parameters[3].Value = item.SortNumber;		                        
		            parameters[4].Value = item.Url;		                        
		            parameters[5].Value = item.Description;		            		            
	            command.CommandText = strSql.ToString();
	         	command.Parameters = parameters;
	         	commands.Add(command);
	         }
            
            int rows=DbHelperSQL.ExecuteSqlTran(commands);
			return rows;

		}
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XQ.SP.Model.BaseCityItem model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [BaseCity] set ");
			                                                
            strSql.Append(" [BaseProvinceID] = @BaseProvinceID , ");                                    
            strSql.Append(" [BaseCityName] = @BaseCityName , ");                                    
            strSql.Append(" [SortNumber] = @SortNumber , ");                                    
            strSql.Append(" [Url] = @Url , ");                                    
            strSql.Append(" [Description] = @Description  ");            			
			strSql.Append(" where [Id]=@Id  ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@BaseCityID", SqlDbType.Int,4) ,            
                        new SqlParameter("@BaseProvinceID", SqlDbType.Int,4) ,            
                        new SqlParameter("@BaseCityName", SqlDbType.VarChar,40) ,            
                        new SqlParameter("@SortNumber", SqlDbType.Int,4) ,            
                        new SqlParameter("@Url", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Description", SqlDbType.NVarChar,1000)             
              
            };
            			            
            parameters[0].Value = model.BaseCityID;                        
            parameters[1].Value = model.BaseProvinceID;                        
            parameters[2].Value = model.BaseCityName;                        
            parameters[3].Value = model.SortNumber;                        
            parameters[4].Value = model.Url;                        
            parameters[5].Value = model.Description;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int BaseCityID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from BaseCity ");
			strSql.Append(" where Id=@Id  ");
						SqlParameter[] parameters = {
					new SqlParameter("@BaseCityID", SqlDbType.Int,4)
			};
			parameters[0].Value = BaseCityID;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string BaseCityIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from BaseCity ");
			strSql.Append(" where Id in ("+BaseCityIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XQ.SP.Model.BaseCityItem GetModel(int BaseCityID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select BaseCityID, BaseProvinceID, BaseCityName, SortNumber, Url, Description  ");			
			strSql.Append("  from BaseCity ");
			strSql.Append(" where Id=@Id  ");
						SqlParameter[] parameters = {
					new SqlParameter("@BaseCityID", SqlDbType.Int,4)
			};
			parameters[0].Value = BaseCityID;

			
			XQ.SP.Model.BaseCityItem model=new XQ.SP.Model.BaseCityItem();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["BaseCityID"].ToString()!="")
				{
					model.BaseCityID=int.Parse(ds.Tables[0].Rows[0]["BaseCityID"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["BaseProvinceID"].ToString()!="")
				{
					model.BaseProvinceID=int.Parse(ds.Tables[0].Rows[0]["BaseProvinceID"].ToString());
				}
																																				model.BaseCityName= ds.Tables[0].Rows[0]["BaseCityName"].ToString();
																												if(ds.Tables[0].Rows[0]["SortNumber"].ToString()!="")
				{
					model.SortNumber=int.Parse(ds.Tables[0].Rows[0]["SortNumber"].ToString());
				}
																																				model.Url= ds.Tables[0].Rows[0]["Url"].ToString();
																																model.Description= ds.Tables[0].Rows[0]["Description"].ToString();
																										
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XQ.SP.Model.BaseCityItem GetModelWhereStr(string whereStr)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select BaseCityID, BaseProvinceID, BaseCityName, SortNumber, Url, Description  ");			
			strSql.Append("  from BaseCity ");
            if (whereStr != "")
            {
                strSql.Append(" where "+whereStr);
            }
			
			XQ.SP.Model.BaseCityItem model=new XQ.SP.Model.BaseCityItem();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["BaseCityID"].ToString()!="")
				{
					model.BaseCityID=int.Parse(ds.Tables[0].Rows[0]["BaseCityID"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["BaseProvinceID"].ToString()!="")
				{
					model.BaseProvinceID=int.Parse(ds.Tables[0].Rows[0]["BaseProvinceID"].ToString());
				}
																																				model.BaseCityName= ds.Tables[0].Rows[0]["BaseCityName"].ToString();
																												if(ds.Tables[0].Rows[0]["SortNumber"].ToString()!="")
				{
					model.SortNumber=int.Parse(ds.Tables[0].Rows[0]["SortNumber"].ToString());
				}
																																				model.Url= ds.Tables[0].Rows[0]["Url"].ToString();
																																model.Description= ds.Tables[0].Rows[0]["Description"].ToString();
																										
				return model;
			}
			else
			{
				return null;
			}
		}
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM BaseCity ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM BaseCity ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
		
	}
}

